package middleware

import (
	"invitation/internal/http/schema"
	"invitation/internal/pojo/dto"

	"github.com/gofiber/fiber/v2"
)

const LocalKeysTeacherCreateRequest = "TEACHER_CREATE_REQUEST"

func (re *RequestExtractor) SetTeacherPayload() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		var teacherReq dto.FirstTimePayload
		if err := ctx.BodyParser(&teacherReq); err != nil {
			return ctx.Status(400).JSON(schema.ErrorResponse{
				Error: &schema.APIError{
					Message: "Invalid request body",
					Code:    400,
				},
			})
		}
		if err := re.validator.Struct(teacherReq); err != nil {
			return ctx.Status(400).JSON(schema.ErrorResponse{
				Error: &schema.APIError{
					Message: "Invalid request body",
					Code:    400,
				},
			})
		}
		teacherReq.Sanitize()
		ctx.Locals(LocalKeysTeacherCreateRequest, &teacherReq)
		return ctx.Next()
	}
}
